﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BugTracker.BTGridView;
using BugTracker.BusinessEntities.Entities.Issue;
using BugTracker.BusinessLogic.BTUser;
using BugTracker.BusinessLogic.Issue;
using BugTracker.BusinessLogic.Project;
using BugTracker.Core.Infrastructure.Exceptions;
using BugTracker.WebUX.WebCode;
using Microsoft.Practices.Unity;

namespace BugTracker.WebUX.Workspace.Modules
{
    public partial class UCIssueResolved : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ModelBTGridView.BTGridViewPageIndexChanging += OnPageIndexChanged;
            if (!Page.IsPostBack)
            {
                BindData(0, ModelBTGridView.PageSize);
            }
        }

        #region methods

        private void OnPageIndexChanged(object sender, BTPagerEventArgs e)
        {
            BindData(e.PageIndex, ModelBTGridView.PageSize);
        }

        private void BindData(int pageIndex, int pageCount)
        {
            int count = 0;
            if (ModelBTGridView.PTSorter == null)
            {
                ModelBTGridView.PTSorter = new Sorter("Name", SortDirection.Ascending);
            }

            string where = "it.ProjectID = " + BTSession.ProjectID.Value.ToString() + "AND it.IsActive = False";
            string order = "it." + ModelBTGridView.PTSorter.Column + " " + ModelBTGridView.PTSorter.GetDirection();

            if (BTSession.ProjectID.HasValue & BTSession.ProjectID != 0)
            {
                var data = Global.IoC.Container.Resolve<IIssueService>()
                    .PageSortWhere(
                                pageIndex,
                                pageCount,
                                where,
                                order,
                                out count
                                );
                ModelBTGridView.VirtualCount = count;
                ModelBTGridView.DataSource = data;
                ModelBTGridView.DataBind();
            }
            else
            {
                var data = Global.IoC.Container.Resolve<IIssueService>()
                    .PageSort(
                                pageIndex,
                                pageCount,
                                where,
                                order,
                                out count
                                );
                ModelBTGridView.VirtualCount = count;
                ModelBTGridView.DataSource = data;
                ModelBTGridView.DataBind();
            }
        }

        #endregion
    }
}